package com.xjcs.saplingplan.ui.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.xjcs.saplingplan.const.getMainAppDefaultTab
import com.xjcs.saplingplan.room.utils.AppUserRoomUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

class AppViewModel(application: Application) : AndroidViewModel(application) {

    val appTabState = MutableStateFlow(getMainAppDefaultTab())

    /**
     * 首页tab
     */
    fun getAppMainTab(userId: String) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                val tabList = AppUserRoomUtils.getAppMainTab(userId)
                viewModelScope.launch(Dispatchers.Main) {
                    appTabState.value = tabList
                }
            } catch (e: Exception) {
                viewModelScope.launch(Dispatchers.Main) {
                    appTabState.value = getMainAppDefaultTab()
                }
            }
        }
    }

}